package com.example.demo;

import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by BG314005 on 2018/7/23.
 */
@Component
public class ApolloOpenApiHelper {
    @Value("${apollo.openapi.url}")
    private String openApiUrl;

    @Value("${apollo.appId}")
    private String app;

    @Value("${apollo.env}")
    private String env;

    @Value("${apollo.cluster}")
    private String cluster;

    @Value("${apollo.openapi.token}")
    private String token;

    @Value("${apollo.openapi.contentType}")
    private String contentType;

    @Autowired
    private RestTemplate restTemplate;

    private HttpEntity httpEntity;

    private HttpHeaders headers;

    private Map<Url, String> urlMap;

    @PostConstruct
    private void init() {
        initHttpEntity();
        initUrlMap();
    }

    private void initUrlMap() {
        urlMap = new HashMap<>();
        urlMap.put(Url.appUrl, openApiUrl + "/apps/" + app);
        urlMap.put(Url.namespaceUrl, openApiUrl + "/envs/" + env + "/apps/" + app + "/clusters/" + cluster + "/namespaces");
    }

    private void initHttpEntity() {
        headers = new HttpHeaders();
        headers.add("Authorization", token);
        headers.add("Content-Type", contentType);
        httpEntity = new HttpEntity(null, headers);
    }

    private enum Url {
        appUrl,
        namespaceUrl;

        private Url() {
        }
    }

    /**
     * 获取App的环境，集群信息
     * @return
     */
    public String getEnvCluters() {
        String url = urlMap.get(Url.appUrl) + "/envclusters";
        ResponseEntity<String> responseEntity = restTemplate
                .exchange(url, HttpMethod.GET, httpEntity, String.class);
        return responseEntity.getBody();
    }

    /**
     * 获取集群下所有Namespace信息接口
     * @return
     */
    public String getNamespaces() {
        String url = urlMap.get(Url.namespaceUrl);
        ResponseEntity<String> responseEntity = restTemplate
                .exchange(url, HttpMethod.GET, httpEntity, String.class);
        return responseEntity.getBody();
    }

    /**
     * 获取某个Namespace信息接口
     * @param namespace
     * @return
     */
    public String getNamespace(String namespace) {
        String url = urlMap.get(Url.namespaceUrl) + "/" + namespace;
        ResponseEntity<String> responseEntity = restTemplate
                .exchange(url, HttpMethod.GET, httpEntity, String.class);
        return responseEntity.getBody();
    }

    /**
     * 创建Namespace
     * @param namespace
     * @param appId
     * @param isPublic
     * @param comment
     * @param createUser
     * @return
     */
    public String createNamespace(String namespace, String appId, boolean isPublic, String comment, String createUser) {
        String url = urlMap.get(Url.appUrl) + "/appnamespaces";
        JsonObject object = new JsonObject();
        object.addProperty("name", namespace);
        object.addProperty("appId", appId);
        object.addProperty("format", "properties");
        object.addProperty("isPublic", isPublic);
        object.addProperty("commnet", comment);
        object.addProperty("dataChangeCreatedBy", createUser);
        HttpEntity httpEntity = new HttpEntity(object, headers);
        ResponseEntity<String> responseEntity = restTemplate
                .exchange(url, HttpMethod.POST, httpEntity, String.class);
        return responseEntity.getBody();
    }
}
